package com.qianbao.qianbaobusiness.ui.cashier;

import com.androidbase.base.mvp.RxMvpPresenter;
import com.androidbase.utils.RxUtils;
import com.baseandroid.config.BossApi;
import com.baseandroid.config.Constant;
import com.baseandroid.retrofit.RxObserver;
import com.baseandroid.retrofit.json.Data;
import com.baseandroid.utils.DESedeEncrypt;
import com.google.gson.Gson;
import com.qianbao.qianbaobusiness.json.AboutSettlementListBean;
import com.qianbao.qianbaobusiness.json.DayBillClassificationListBean;

import java.util.HashMap;

import javax.inject.Inject;

import timber.log.Timber;

public class DataBillPresenter extends RxMvpPresenter<DataBillView>{
    BossApi myApi ;

    @Inject
    public DataBillPresenter(BossApi myApi) {
        this.myApi = myApi;
    }

    public void getQueryDayChecking(String chkAccountDate, String postMerchantNo) {
        HashMap<String, String> hashMap = new HashMap<>();
        hashMap.put("chkAccountDate", chkAccountDate);// 账单日期
        hashMap.put("mCode", postMerchantNo);// 商户代码

        myApi.getQueryDayChecking( Constant.encryptMap(hashMap))
                .compose(RxUtils.applySchedulersLifeCycle(getView()))
                .subscribe(new RxObserver<Data<String>>(){
                    @Override
                    public void onNext(Data<String> stringData) {
                        if(RxObserver.checkJsonCode(stringData,false)){
                            try {
                                String result = DESedeEncrypt.decrypt("zm39nfhzKZ7EC4AskZSM1UBtDtlXJZ0I"
                                        , stringData.getResult());
                                // new Gson().fromJson()
                                DayBillClassificationListBean bean = new Gson().fromJson(result
                                        ,DayBillClassificationListBean.class);
                                Timber.e("result = " + result);
//                                getView().onSuccess(bean);
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                    }
                });
    }

    public void getSettleDetail(String chkAccountDate, String merchantCode) {
        HashMap<String, String> hashMap = new HashMap<>();
        hashMap.put("settleDate", chkAccountDate);// 账单日期
        hashMap.put("mCode", merchantCode);// 商户代码

        myApi.getSettleDetail(Constant.encryptMap(hashMap))
                .compose(RxUtils.applySchedulersLifeCycle(getView()))
                .subscribe(new RxObserver<Data<String>>(){
                    @Override
                    public void onNext(Data<String> stringData) {
                        if(RxObserver.checkJsonCode(stringData,false)){
                            try {
                                String result = DESedeEncrypt.decrypt("zm39nfhzKZ7EC4AskZSM1UBtDtlXJZ0I"
                                        , stringData.getResult());
                                // new Gson().fromJson()
                                AboutSettlementListBean bean = new Gson().fromJson(result
                                        ,AboutSettlementListBean.class);
                                Timber.e("result = " + result);
                                getView().onSuccess(bean);
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                    }
                });
    }
}
